package com.example.tianyutong.qanda.adapter;

import android.app.Activity;
import android.text.TextUtils;
import android.view.View;

import com.example.tianyutong.qanda.R;
import com.example.tianyutong.qanda.base.binding.BaseBindingAdapter;
import com.example.tianyutong.qanda.base.binding.BaseBindingHolder;
import com.example.tianyutong.qanda.bean.GankIoDataBean;
import com.example.tianyutong.qanda.databinding.ItemAndroidBinding;
import com.example.tianyutong.qanda.utils.GlideUtil;
import com.example.tianyutong.qanda.view.webview.WebViewActivity;

/**
 * Created by tianyutong on 2016/12/2.
 */

public class SearchAdapter extends BaseBindingAdapter<GankIoDataBean.ResultBean, ItemAndroidBinding> {

    private boolean isAll = false;

    public void setAllType(boolean isAll) {
        this.isAll = isAll;
    }

    public SearchAdapter(Activity activity) {
        super(R.layout.item_android);
    }

    @Override
    protected void bindView(BaseBindingHolder holder, GankIoDataBean.ResultBean object, ItemAndroidBinding binding, int position) {
        if (object != null) {
//            if (isAll && "福利".equals(object.getType())) {
//                binding.ivAllWelfare.setVisibility(View.VISIBLE);
//                binding.llWelfareOther.setVisibility(View.GONE);
//                GlideUtil.displayEspImage(object.getUrl(), binding.ivAllWelfare, 1);
//            } else {
//                binding.ivAllWelfare.setVisibility(View.GONE);
//                binding.llWelfareOther.setVisibility(View.VISIBLE);
//            }

            if (isAll) {
                binding.tvContentType.setVisibility(View.VISIBLE);
                binding.tvContentType.setText(" · " + object.getType());
            } else {
                binding.tvContentType.setVisibility(View.GONE);

            }

            // 显示gif图片会很耗内存
            if (object.getImages() != null
                    && object.getImages().size() > 0
                    && !TextUtils.isEmpty(object.getImages().get(0))) {
                binding.questionPic.setVisibility(View.VISIBLE);
                GlideUtil.displayGif(object.getImages().get(0), binding.questionPic);
                //                Glide.with(context).load(object.getImages().get(0))
//                        .diskCacheStrategy(DiskCacheStrategy.SOURCE)
//                        .placeholder(R.drawable.img_one_bi_one)
//                        .error(R.drawable.img_one_bi_one)
//                        .into(binding.ivAndroidPic);
            } else {
                binding.questionPic.setVisibility(View.GONE);
            }
            binding.llAll.setOnClickListener(v -> WebViewActivity.loadUrl(v.getContext(), object.getUrl(), object.getDesc()));

            binding.setResultsBean(object);
            binding.executePendingBindings();
        }
    }
}
